package fabric

import (
	"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
	"sync"
)

var (
	once sync.Once

	info SdkEnvInfo
	sdk  *fabsdk.FabricSDK
)

func MspRegisterUser(user, orgName string) (string, error) {
	return registerUser(&info, user, orgName)
}

func BuildSDKOnce() error {
	var err error

	once.Do(func() {
		orgs := []*OrgInfo{
			{
				OrgAdminUser: "Admin",
				OrgName:      "Orderer",
				OrgMspId:     "OrdererMSP",
				OrgUser:      "User1",
				//OrgPeerNum:    1,
				//OrgAnchorFile: os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric-samples/test-network/channel-artifacts/Org1MSPanchors.tx",
			},
			{
				OrgAdminUser: "Admin",
				OrgName:      "OrgGo",
				OrgMspId:     "OrgGoMSP",
				OrgUser:      "User1",
				OrgPeerNum:   500,
				//OrgAnchorFile: os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric-samples/test-network/channel-artifacts/Org2MSPanchors.tx",
			},
			{
				OrgAdminUser: "Admin",
				OrgName:      "OrgCPP",
				OrgMspId:     "OrgCppMSP",
				OrgUser:      "User1",
				OrgPeerNum:   1,
				//OrgAnchorFile: os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric-samples/test-network/channel-artifacts/Org2MSPanchors.tx",
			},
		}

		// init sdk env info
		info = SdkEnvInfo{
			//ChannelID:        "testchannel",
			//ChannelConfig:    os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric-samples/test-network/channel-artifacts/testchannel.tx",
			Orgs:             orgs,
			OrdererAdminUser: "Admin",
			OrdererOrgName:   "OrdererOrgs",
			OrdererEndpoint:  "Orderer.zhaowu.com",
			//ChaincodeID:      cc_name,
			//ChaincodePath:    os.Getenv("GOPATH")+"/src/github.com/shuizhongmose/go-fabric/fabric-first-go-app/chaincode/",
			//ChaincodeVersion: cc_version,
		}

		// sdk setup
		sdk, err = Setup("msp.yaml", &info)
		if err != nil {
			return
		}
	})

	return err
}
